package com.it.config;



import com.it.common.JacksonObjectMapper;
import com.it.interceptor.LoginInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
//@EnableWebMvc
@Slf4j
public class SpringMvcConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/back/**")
                .addResourceLocations("classpath:/back/");
        log.info("静态资源放行...");
    }
    @Autowired
    private LoginInterceptor loginInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**") // 拦截所有请求
                .excludePathPatterns(
                        "/admin/login",   // 登录接口不拦截
                        "/admin/logout",  // 登出接口不拦截
                        "/error",         // 错误页面
                        "/static/**",     // 静态资源
                        "/css/**",
                        "/js/**",
                        "/images/**"
                );
    }
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 默认的json转换的转换器
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        // 把自定义的转换格式覆盖掉原来的格式
        messageConverter.setObjectMapper(new JacksonObjectMapper());
        // 把自定义转化转化器加入到mvc容器中 第一顺位执行
        converters.add(0,messageConverter);
    }
}
